var utils = require('../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;

function initSubMenuDisplay() {
    return ['hidden', 'hidden', 'hidden'];
}

function colorSubMenuDisplay() {
    return ['', '', ''];
}

function isIconMenuDisplay() {
    return [false, false, false];
}
var initSubMenuHighLight = [
    ['', '', '', '', ''],

    ['', ''],

    ['', '', '']
];
Page({
    data: {
        subMenuDisplay: initSubMenuDisplay(),
        subcolorSubMenuDisplay: colorSubMenuDisplay(),
        isStopBodyScroll: false,
        iconMenuDisplay: isIconMenuDisplay(),
        videoimage: "block", //默认显示封面
        bindplay: null,
        _index: 0,
        isStart: false,
        curBdIndex: 0, //点击收藏状态
        class_id:"",
        currBut:0,
        pid:""
    },
    onLoad: function(option) {
        var that = this;
        wx.getSystemInfo({
            success: function(res) {
                console.log(res.windowHeight)
                that.setData({
                    windowHeight: res.windowHeight
                })
            },
        })
        // that.categoryList();
        that.listName(that.data.pid);
        
    },
    onShow:function(e){
        var that=this;
        that.setData({
            class_id: "",
        })
        that.videoList(that.data.class_id);
    },
    //2文章分类
    categoryList:function(pid){
        var that = this;
        utils.http(httpUrl + "/mobile/article/category_list_v2", {pid:pid}, function (res) {
            if (res.state == 1) {
                // console.log(res);
                that.setData({
                    expertclass: res.datas,
                });
                that.listName11(res.datas[0].id);
            } else {
                utils.message(res.datas);
            }
        })
    },

    tapMainMenu: function(e) { 
        
        // 获取当前显示的一级菜单标识
        var index = parseInt(e.currentTarget.dataset.index); // 生成数组,全为hidden的,只对当前的进行显示
        var id = parseInt(e.currentTarget.dataset.id); 
        this.setData({
            in: id,
            indexfirst: index
        })
        this.categoryList(id);
        
        var newSubMenuDisplay = initSubMenuDisplay(); // 如果目前是显示则隐藏,反之亦反之。同时要隐藏其他的菜单
        var newcolorSubMenuDisplay = colorSubMenuDisplay(); //字体颜色
        var newiconMenuDisplay = isIconMenuDisplay(); //小图标切换
        if (this.data.subMenuDisplay[index] == 'hidden') {
            newSubMenuDisplay[index] = 'show';
            newcolorSubMenuDisplay[index] = 'color';
            newiconMenuDisplay[index] = true
            this.setData({
                isStopBodyScroll: true,
            })
        } else {
            newSubMenuDisplay[index] = 'hidden';
            newcolorSubMenuDisplay[index] = '';
            newiconMenuDisplay[index] = false
            this.setData({
                isStopBodyScroll: false
            })

        } // 设置为新的数组
        this.setData({
            subMenuDisplay: newSubMenuDisplay,
            subcolorSubMenuDisplay: newcolorSubMenuDisplay,
            iconMenuDisplay: newiconMenuDisplay
        });
    },
    tapSubMenu: function(e) {
        var that=this;
        // 隐藏所有一级菜单   
        this.setData({
            subMenuDisplay: initSubMenuDisplay(),
            subcolorSubMenuDisplay: colorSubMenuDisplay(),
            subMenuDisplay: initSubMenuDisplay(),
            isStopBodyScroll: false
        })
        var newcolorSubMenuDisplay = colorSubMenuDisplay(); //字体颜色
        var newiconMenuDisplay = isIconMenuDisplay(); //小图标切换

        // 处理二级菜单，首先获取当前显示的二级菜单标识      
        var indexArray = e.currentTarget.dataset.index.split('-');
        var id = e.currentTarget.dataset.id;
        var idx = e.currentTarget.dataset.idx;
            that.setData({
                class_id:id
            })
        
        that.videoList(that.data.class_id);
       // 初始化状态        
        // var newSubMenuHighLight = initSubMenuHighLight;       
        for (var i = 0; i < initSubMenuHighLight.length; i++) {
            // 如果点中的是一级菜单，则先清空状态，即非高亮模式，然后再高亮点中的二级菜单；如果不是当前菜单，而不理会。经过这样处理就能保留其他菜单的高亮状态           
            if (indexArray[0] == i) {
                for (var j = 0; j < initSubMenuHighLight[i].length; j++) {
                    // 实现清空                  
                    initSubMenuHighLight[i][j] = '';
                }
                // 将当前菜单的二级菜单设置回去         
            }
        }
        // 与一级菜单不同，这里不需要判断当前状态，只需要点击就给class赋予highlight即可        
        initSubMenuHighLight[indexArray[0]][indexArray[1]] = 'highlight'; // 设置为新的数组       
        this.setData({
            subMenuHighLight: initSubMenuHighLight,
            subcolorSubMenuDisplay: "",
            iconMenuDisplay: false
        });
    },
    menuTap:function(e){
        console.log(e);
        var that=this;
        var index=e.currentTarget.dataset.index;
        var id = e.currentTarget.dataset.id;
        console.log(id);
        that.setData({
            currBut: index
        })
        that.listName11(id);
    },
     //三级分类列表
    listName11: function (pid) {
        var that = this;
        utils.http(httpUrl + "/mobile/article/category_list_v2", { pid: pid }, function (res) {
            console.log(res);
            that.setData({
                listlast: res.datas
            })
        })
    },
    //yi级分类列表
    listName:function(pid){
        var that=this;
        utils.http(httpUrl + "/mobile/article/category_list_v2", {pid:pid},function(res){
                console.log(res);
                that.setData({
                    listName:res.datas
                })
        })
    },
    //进入热推文章
    oNNewMoreTap: function(e) {
        //1-公告，2-文章，3-视频，4-兴趣班新闻5-对比标准（个人推送新闻）
        wx.navigateTo({
            url: '../newmore/newmore?type=2',
        })
    },
    //进入视频
    oNaudioTap: function(e) {
        wx.navigateTo({
            url: '../motion/motionAudio/motionAudio',
        })
    },
    //列表显示
    videoList: function (class_id) {
        var that = this;
        utils.showLoading();
        utils.http(httpUrl + "/mobile/article/index_v2", { class_id: class_id}, function(res) {
            utils.hideLoading();
            console.log(res);
            if (res.state == 1) {
                var dataList = [];
                for (var i in res.datas.article_list) {
                    var time = Date.parse(new Date(res.datas.article_list[i].create_time));
                    var timeData1 = utils.getPastTime(time);
                    var port = {
                        id: res.datas.article_list[i].id,
                        title: res.datas.article_list[i].title,
                        photo: res.datas.article_list[i].photo,
                        reading: res.datas.article_list[i].reading,
                        col_num: res.datas.article_list[i].col_num,
                        zan_num: res.datas.article_list[i].zan_num,
                        create_time: timeData1

                    }
                    dataList.push(port);
                }
                that.setData({
                    slideList: dataList,
                    videolist: res.datas.video_list
                });
            } else {
                utils.message(res.datas);
            }
        })

    },
    //进入新闻详情
    noNewmoreDetailTap: function(e) {
        wx.navigateTo({
            url: '../newmore/newmoreDetail/newmoreDetail?id=' + e.currentTarget.dataset.id+"&type=2",
        })
    },

    //点击播放按钮，封面图片隐藏,播放视频
    bindplay: function(e) {
        var that = this;
        var index = e.currentTarget.dataset.id;
        var itemid = e.currentTarget.dataset.itemid;
        //停止正在播放的视频
        var videoContextPrev = wx.createVideoContext("myVideo" + that.data._index)
        videoContextPrev.seek(0)
        videoContextPrev.pause();
        // setTimeout(function () {
        //     //将点击视频进行播放
        //     var videoContext = wx.createVideoContext('myVideo' + index)
        //     videoContext.play();
        //     that.data._index = index;
        // }, 100)
        utils.showLoading()
        console.log();
        utils.http(httpUrl + "/mobile/article/video_article_detail", {
            id: itemid
        }, function(res) {
            utils.hideLoading();
            if (that.data.videolist[index].price <= 0) {
                that.setData({
                    videoDetail: res.datas
                })
                that.setData({
                    id: index
                })
                var videoContext = wx.createVideoContext('myVideo' + index)
                videoContext.play();
                that.data._index = index;

            } else {
                wx.showModal({
                    title: '提示',
                    content: res.datas,
                    success(res) {
                        if (res.confirm) {
                            utils.http(httpUrl + "/mobile/article/add_order", {
                                id: itemid
                            }, function(res) {
                                wx.requestPayment({
                                    timeStamp: res.timeStamp,
                                    nonceStr: res.nonceStr,
                                    package: res.package,
                                    signType: res.signType,
                                    paySign: res.paySign,
                                    success(res) {
                                        wx.showToast({
                                            title: "支付成功",
                                            icon: 'success',
                                            duration: 2000
                                        })
                                        that.setData({
                                            id: index
                                        })
                                        var videoContext = wx.createVideoContext('myVideo' + index)
                                        videoContext.play();
                                        that.data._index = index
                                    },
                                    fail(res) {
                                        that.setData({
                                            id: -0
                                        })
                                    }
                                })
                            })

                        } else if (res.cancel) {
                            that.setData({
                                id: -0
                            })
                        }
                    }
                })

            }

        })

    },

    //视频收藏
    oNstrartTap: function(e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        for (var i in that.data.videolist) {
            if (that.data.videolist[i].id == id) {
                utils.http(httpUrl + "/mobile/common/make_col", {
                    be_id: id,
                    type: 3
                }, function(res) {
                    if (res.state == 1) {
                        utils.message(res.datas);
                        that.videoList(that.data.class_id);
                    } else {
                        utils.message(res.datas);
                    }
                })
            } else {
                // console.log("111111111111111111111");
            }
        }

    },

    //视频点赞
    oNzanTap: function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        for (var i in that.data.videolist) {
            if (that.data.videolist[i].id == id) {
                utils.http(httpUrl + "/mobile/common/make_zan", {
                    be_id: id,
                    type: 3
                }, function (res) {
                    if (res.state == 1) {
                        utils.message(res.datas);
                        that.videoList(that.data.class_id);
                    } else {
                        utils.message(res.datas);
                    }
                })
            } else {
                // console.log("111111111111111111111");
            }
        }

    },
});